package wang.kentwang.servlet;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundAuthOrderUnfreezeRequest;
import com.alipay.api.response.AlipayFundAuthOrderUnfreezeResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Test extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了servlet");
        //获取aliapp应用
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                "20191007XXXXXXXXx",
                //应用私钥
                "your privateKey",
                "json",
                "utf-8",
                //支付宝公钥
                "your alipayPublickey",
                "RSA2");
        System.out.println("进入了执行第1条语句");
        //new用户订单解冻请求
        AlipayFundAuthOrderUnfreezeRequest request = new AlipayFundAuthOrderUnfreezeRequest();
        System.out.println("进入了执行第2条语句");
        //设置请求参数
        request.setBizContent("{" +
                "\"auth_no\":\"2019122610002001490569802100\"," +
                "\"out_request_no\":\"157734324545600\"," +
                "\"amount\":670.00," +
                "\"remark\":\"解冻700的押金\"," +
                "\"extra_param\":\"{\\\"unfreezeBizInfo\\\": \\\"{\\\\\\\"bizComplete\\\\\\\":\\\\\\\"true\\\\\\\"}\\\"}\"" +
                "  }");
        System.out.println("进入了执行第3条语句");
        //设置响应
        AlipayFundAuthOrderUnfreezeResponse response = null;
        System.out.println("进入了执行第4条语句");
        try {
            //执行请求获得响应
            System.out.println("开始执行请求");
            response = alipayClient.execute(request);
            System.out.println("执行请求完毕");
        } catch (AlipayApiException e) {
            System.out.println("执行请求异常");
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
